package com.example.restaurant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.controller.form.AddDeskForm;
import com.example.restaurant.controller.form.GetDeskForm;
import com.example.restaurant.controller.form.PutDeskForm;
import com.example.restaurant.model.Desk;
import com.example.restaurant.dao.DeskMapper;
import com.example.restaurant.service.DeskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * <p>
 * 桌表 服务实现类
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@Service
public class DeskServiceImpl extends ServiceImpl<DeskMapper, Desk> implements DeskService {

    @Resource
    private DeskMapper deskMapper;

    @Override
    public boolean checkDeskName(String deskName) {
        QueryWrapper<Desk> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("desk_name",deskName);
        Desk desk = deskMapper.selectOne(queryWrapper);
        if(desk!=null){
            throw new RestaurantException("桌名不可重复");
        }
        return true;
    }

    //========未完成===========
    //新增餐桌
    @Transactional
    @Override
    public boolean addDesk(AddDeskForm addDeskForm) {
        Desk desk = BeanUtil.copyProperties(addDeskForm, Desk.class);
        //String simpleUUID = IdUtil.simpleUUID();
       // String codeName = simpleUUID+".jpg";
       // QrCodeUtil.generate("https://hutool.cn/", 300, 300, FileUtil.file("d:/Code/"+codeName));
       // desk.setCode(codeName);
        int insert = deskMapper.insert(desk);
        if(insert>0){
            return true;
        }else{
            throw new RestaurantException("新增餐桌失败");
        }
    }

    //删除餐桌
    @Override
    public boolean delDesk(int deskId) {
        QueryWrapper<Desk> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",deskId);
        int result = deskMapper.delete(queryWrapper);
        if(result>0){
            return true;
        }else{
            throw new RestaurantException("删除餐桌失败");
        }
    }

    //查询餐桌
    public Page<Desk> getDeskByStoreId(Integer storeId,String deskName,int current,int size){
        if(storeId<1){
            throw new RestaurantException("storeId不能为空");
        }
        QueryWrapper<Desk> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",storeId);
        queryWrapper.like(deskName!=null,"desk_name",deskName);
        Page<Desk> deskPage = deskMapper.selectPage(new Page<>(current, size), queryWrapper);
        if(deskPage.getRecords().size()==0){
            throw new RestaurantException("未查询到该门店下的餐桌信息");
        }
        return deskPage;
    }

    @Override
    @Transactional
    public boolean putDesk(PutDeskForm putDeskForm) {
        Desk desk = BeanUtil.copyProperties(putDeskForm, Desk.class);
        QueryWrapper<Desk> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",putDeskForm.getId());
        int update = deskMapper.update(desk, queryWrapper);
        if(update<1){
            throw new RestaurantException("修改餐桌信息失败");
        }
        return true;
    }

    @Override
    public List<Desk> getDeskByStoreIda(int storeId) {
        QueryWrapper<Desk> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",storeId);
        List<Desk> desks = deskMapper.selectList(queryWrapper);
        return desks;
    }


}
